//
//  CollisionListener.cpp
//  airfight
//
//  Created by USER on 12-9-27.
//  Copyright (c) 2012年 moonriver. All rights reserved.
//

#include "CollisionListener.h"
#include "EntitySprite.h"
#include "PlayerFighterSprite.h"
#include "BulletSprite.h"
#include "EnemyFighterSprite.h"
#include "EnemyBulletSprite.h"
#include "BonusSprite.h"

CollisionListener::CollisionListener() : m_Contacts() 
{
}

CollisionListener::~CollisionListener() 
{
}

void CollisionListener::BeginContact(b2Contact* contact) 
{
//    CollisionContact curContact = {contact->GetFixtureA(), contact->GetFixtureB()};
//    m_Contacts.push_back(curContact);
    
    void* bodyUserData_A = contact->GetFixtureA()->GetBody()->GetUserData();
    void* bodyUserData_B = contact->GetFixtureB()->GetBody()->GetUserData();
    
    if(bodyUserData_A && bodyUserData_B)
    {
        EntitySprite* entity_A = static_cast<EntitySprite*>( bodyUserData_A );
        EntitySprite* entity_B = static_cast<EntitySprite*>( bodyUserData_B );
        if(entity_A->isVisible() && entity_B->isVisible())
        {
            ENTITY_TYPE type_A = entity_A->getEntityType();
            ENTITY_TYPE type_B = entity_B->getEntityType();
//            PLAYER_FIGHTER = 0,
//            PLAYER_BULLET,
//            ENEMY_FIGHTER,
//            ENEMY_BULLET
            //小飞机与敌机相碰
            if((type_A == PLAYER_FIGHTER && type_B == ENEMY_FIGHTER) || (type_A == ENEMY_FIGHTER && type_B == PLAYER_FIGHTER))
            {
                PlayerFighterSprite* player_sprite = NULL;
                EnemyFighterSprite* enemy_sprite = NULL;
                switch (type_A) {
                    case PLAYER_FIGHTER:
                    {
                        player_sprite = static_cast<PlayerFighterSprite*>( bodyUserData_A );
                        enemy_sprite = static_cast<EnemyFighterSprite*>( bodyUserData_B );
                        break;
                    }
                    case ENEMY_FIGHTER:
                    {
                        player_sprite = static_cast<PlayerFighterSprite*>( bodyUserData_B );
                        enemy_sprite = static_cast<EnemyFighterSprite*>( bodyUserData_A );
                        break;
                    }
                    default:
                        break;
                }
                CCDictionary* effect = CCDictionary::create();
                effect->setObject(CCInteger::create(enemy_sprite->getBlood()),"damage");
                effect->setObject(CCInteger::create(enemy_sprite->getTag()),"tag");
                player_sprite->handleContact(ENEMY_FIGHTER,effect);
                
               
            }
            
            //小飞机与敌机子弹相碰
            else if((type_A == PLAYER_FIGHTER && type_B == ENEMY_BULLET) || (type_A == ENEMY_BULLET && type_B == PLAYER_FIGHTER))
            {
                PlayerFighterSprite* player_sprite = NULL;
                EnemyBulletSprite* enemyBullet_sprite = NULL;
                switch (type_A) {
                    case PLAYER_FIGHTER:
                    {
                        player_sprite = static_cast<PlayerFighterSprite*>( bodyUserData_A );
                        enemyBullet_sprite = static_cast<EnemyBulletSprite*>( bodyUserData_B );
                        break;
                    }
                    case ENEMY_BULLET:
                    {
                        player_sprite = static_cast<PlayerFighterSprite*>( bodyUserData_B );
                        enemyBullet_sprite = static_cast<EnemyBulletSprite*>( bodyUserData_A );
                        break;
                    }
                    default:
                        break;
                }
                CCDictionary* effect = CCDictionary::create();
                effect->setObject(CCInteger::create(enemyBullet_sprite->getDamage()),"damage");
                effect->setObject(CCInteger::create(enemyBullet_sprite->getTag()),"tag");
                player_sprite->handleContact(ENEMY_BULLET,effect);
                
            }
            
            //敌机与小飞机子弹相碰
            else if((type_A == ENEMY_FIGHTER && type_B == PLAYER_BULLET) || (type_A == PLAYER_BULLET && type_B == ENEMY_FIGHTER))
            {
                EnemyFighterSprite* enemy_sprite = NULL;
                BulletSprite* bullet_sprite = NULL;
                switch (type_A) {
                    case ENEMY_FIGHTER:
                    {
                        enemy_sprite = static_cast<EnemyFighterSprite*>( bodyUserData_A );
                        bullet_sprite = static_cast<BulletSprite*>( bodyUserData_B );
                        break;
                    }
                    case PLAYER_BULLET:
                    {
                        enemy_sprite = static_cast<EnemyFighterSprite*>( bodyUserData_B );
                        bullet_sprite = static_cast<BulletSprite*>( bodyUserData_A );
                        break;
                    }
                    default:
                        break;
                }
                CCDictionary* effect = CCDictionary::create();
                effect->setObject(CCInteger::create(bullet_sprite->getDamage()),"damage");
                effect->setObject(CCInteger::create(bullet_sprite->getTag()),"tag");
                enemy_sprite->handleContact(PLAYER_BULLET,effect);
            }
            //小飞机与掉落物碰撞
            else if((type_A == PLAYER_FIGHTER && type_B == BONUS) || (type_A == BONUS && type_B == PLAYER_FIGHTER))
            {
                BonusSprite* bonus_sprite = NULL;
                switch (type_A) {
                    case PLAYER_FIGHTER:
                    {
                        bonus_sprite = static_cast<BonusSprite*>( bodyUserData_B );
                        break;
                    }
                    case ENEMY_BULLET:
                    {
                        bonus_sprite = static_cast<BonusSprite*>( bodyUserData_A );
                        break;
                    }
                    default:
                        break;
                }
                CCDictionary* effect = CCDictionary::create();
                effect->setObject(CCInteger::create(bonus_sprite->getTag()),"tag");
                bonus_sprite->handleContact(PLAYER_FIGHTER,effect); 
            }
            
        }
    }
}

void CollisionListener::EndContact(b2Contact* contact) 
{
//    CollisionContact curContact = { contact->GetFixtureA(), contact->GetFixtureB() };
//    std::vector<CollisionContact>::iterator pos;
//    pos = std::find(m_Contacts.begin(), m_Contacts.end(), curContact);
//    if (pos != m_Contacts.end())
//    {
//        m_Contacts.erase(pos);
//    }
}

void CollisionListener::PreSolve(b2Contact* contact, const b2Manifold* oldManifold) 
{
    
}

void CollisionListener::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) 
{
}
